﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 一事一议项目审核
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class OnePrjOneDiscusshController : AdminBaseController
    {
        //
        // GET: /Admin/OnePrjOneDiscussh/
        /// <summary>
        /// 一事一议项目审核主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_OnePrjOneDiscussh", Order = 1)]
        [LogPageActionFilter("一事一议项目审核 ", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult OnePrjOneDiscusshList() {
            int pageIndex = 1;
            List<Model.OnePrjOneDiscus> onePrjOneDiscusshList = GetOnePrjOneDiscusshList(pageIndex);

            return View(onePrjOneDiscusshList);
        
        }

        /// <summary>
        /// 用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult OnePrjOneDiscusshListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());

            string id = Request.Params["id"];
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }


            List<Model.OnePrjOneDiscus> OnePrjOneDiscusshList = GetOnePrjOneDiscusshList(pageIndex, pageCount, pageRowSize);


            return PartialView("OnePrjOneDiscusshListPartial", OnePrjOneDiscusshList);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageCount">每页显示大小</param>
        /// <param name="pageRowSize">每页显示页数</param>
        /// <returns>一事一议列表</returns>
        private List<Model.OnePrjOneDiscus> GetOnePrjOneDiscusshList(int pageIndex, int pageCount = 0, int pageRowSize = 0)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            string fiedls = "*";
            int total;
            string where = "";

            DataSet ds = commonBll.GetListByPage("v_OnePrjOneDiscus", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, where, "FOnePrjOneDiscusID", 1, out total);

            List<Model.OnePrjOneDiscus> onePrjOneDiscusshAllList = new List<Model.OnePrjOneDiscus>();
            List<Model.OnePrjOneDiscus> onePrjOneDiscusshList = onePrjOneDiscusBll.DataTableToListOnePrjOneDiscus(ds.Tables[0]);

            pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                onePrjOneDiscusshAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (onePrjOneDiscusshList.Count > i)
                {
                    onePrjOneDiscusshAllList[((pageIndex - 1) * pageRowSize) + i] = onePrjOneDiscusshList[i];
                }
            }

            return onePrjOneDiscusshAllList;
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        /// <param name="id">一事一议ID</param>
        /// <returns>视图</returns>
        public ActionResult OnePrjOneDiscusshContent(string id) {

            Model.OnePrjOneDiscus onePrjOneDiscusModel = new Model.OnePrjOneDiscus();
            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            id = Fetch.estimateString(id, CheckGetEnum.Safety);
            onePrjOneDiscusModel = onePrjOneDiscusBll.GetModel(decimal.Parse(id));
            onePrjOneDiscusModel.FStatus = "N";
            ViewData["id"] = id;

            return View(onePrjOneDiscusModel);
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public string DeleteMany(string ids)
        {
            string str = "";
            string strEntityNo = GlobalConstant.ENTITYNO;
            string[] idArray = ids.Split(new string[] { "%!^" }, StringSplitOptions.RemoveEmptyEntries);
            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            List<string> docPathList = GetManyPathList(idArray, strEntityNo);
            try
            {               
                onePrjOneDiscusBll.DeleteMany(idArray);
                DeleteDoc(docPathList);//删除保存在硬盘的图片
                str = "ok";
            }
            catch (Exception)
            {
                str = "error";
            }

            return str;

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public string Del(string id)
        {
            string str = "";
            string strEntityNo = GlobalConstant.ENTITYNO;
            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            List<string> docPathList = GetPathList(id, strEntityNo);//获取保存在硬盘中的图片路径list

            try
            {
                onePrjOneDiscusBll.Delete(decimal.Parse(id));
                DeleteDoc(docPathList);//删除保存在硬盘的图片
                str = "ok";
            }
            catch (Exception)
            {
                str = "error";
            }

            return str;

        }

        /// <summary>
        /// 获取保存在硬盘中的图片路径
        /// </summary>
        /// <param name="strAdCds">广告编号</param>
        /// <param name="strEntityNo">公司编号</param>
        /// <returns>图片路径</returns>

        public List<string> GetPathList(string id, string strEntityNo)
        {
            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            List<string> strList = onePrjOneDiscusBll.GetSrvName(id, strEntityNo);
            return strList;
        }
        /// <summary>
        /// 删除保存在硬盘的图片
        /// </summary>
        /// <param name="strList">图片路径list</param>
        public void DeleteDoc(List<string> strList)
        {
            foreach (string strPath in strList)
            {
                GenerateUtil.DeleteFile(Url.Content("~") + strPath);
            }
        }
        /// <summary>
        /// 获取保存在硬盘中的图片路径 批量删除
        /// </summary>
        /// <param name="strAdCds">广告编号</param>
        /// <param name="strEntityNo">公司编号</param>
        /// <returns>图片路径</returns>

        public List<string> GetManyPathList(string[] ids, string strEntityNo)
        {
            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            List<string> strList = onePrjOneDiscusBll.GetManySrvName(ids, strEntityNo);
            return strList;
        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="onePrjOneDiscusModel">onePrjOneDiscusModel对象</param>
        /// <returns>视图</returns>
        public ActionResult Check(Model.OnePrjOneDiscus onePrjOneDiscusModel)
        {
            onePrjOneDiscusModel.FOnePrjOneDiscusID = decimal.Parse(Fetch.Post("hidid"));
            onePrjOneDiscusModel.FCheckEmpNo = OperateSession.get("FEmplNo");
            onePrjOneDiscusModel.FCheckIP = Common.GenerateUtil.GetClientIPv4Address();
            onePrjOneDiscusModel.FCheckDate = DateTime.Now;
            onePrjOneDiscusModel.FStatus = onePrjOneDiscusModel.FisPoor = RadioButtonListExtension.GetValue<string>("tbFStatus");

            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            onePrjOneDiscusBll.Check(onePrjOneDiscusModel);

            ViewData["message"] = "审核操作成功";
            ViewData["IsClose"] = "Y";

            return View("OnePrjOneDiscusshContent", onePrjOneDiscusModel);
        }



        /// <summary>
        /// 详细信息界面
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>视图</returns>
        public ActionResult OnePrjOneDiscusshInformation(string id)
        {

            Model.OnePrjOneDiscus onePrjOneDiscusModel = new Model.OnePrjOneDiscus();
            BLL.OnePrjOneDiscus onePrjOneDiscusBll = new BLL.OnePrjOneDiscus();
            id = Fetch.estimateString(id, CheckGetEnum.Safety);
            onePrjOneDiscusModel = onePrjOneDiscusBll.GetModel(decimal.Parse(id));

            return View(onePrjOneDiscusModel);
        
        }



    }
}
